"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @desc    模板管理
 * @author  刘贺[of2220]
 * @company qianmi.com
 * @Date    2017/2/10
 */
('use strict');
const mongoose = require("mongoose");
const template_info_model_1 = require("../dao/templateInfo/template-info-model");
class TemplateInfoService {
    //保存templateInfo templateCode唯一ID
    static upsertTplInfo(templateInfo) {
        let conditions = { templateCode: templateInfo.templateCode };
        let options = { upsert: true };
        delete templateInfo._id;
        return new Promise((resolve, reject) => {
            template_info_model_1.default.update(conditions, templateInfo, options).exec((err, res) => {
                err ? reject(err) : resolve(res);
            });
        });
    }
    static getTemplateInfoByIdOrCode(id) {
        let conditions = mongoose.Types.ObjectId.isValid(id)
            ? { $or: [{ _id: id }, { templateCode: id }] }
            : { templateCode: id };
        let selectOpt = '-acceptBars -pluginBars -adapterBars -dependenciesBar -barDefaultPropsMap -nsTransferMap -children -templateConfig';
        return new Promise((resolve, reject) => {
            template_info_model_1.default.findOne(conditions).select(selectOpt).exec((err, res) => {
                err ? reject(err) : resolve(res);
            });
        });
    }
    static async getTemplateList({ systemCode, pageNo = -1, pageSize = 10, isShow, platform, includePageTypeList = [], excludePageTypeList = [] }) {
        let conditions = { systemCode };
        if (typeof isShow === 'boolean') {
            Object.assign(conditions, { isShow });
        }
        platform && Object.assign(conditions, { platform });
        includePageTypeList &&
            includePageTypeList.length &&
            Object.assign(conditions, { pageType: { $in: includePageTypeList } });
        excludePageTypeList &&
            excludePageTypeList.length &&
            Object.assign(conditions, { pageType: { $nin: excludePageTypeList } });
        let selectOpt = '-acceptBars -pluginBars -adapterBars -dependenciesBar -barDefaultPropsMap -nsTransferMap -children -templateConfig';
        try {
            let totalCount = await new Promise((resolve, reject) => {
                template_info_model_1.default.count(conditions).exec((err, res) => {
                    err ? reject(err) : resolve(res);
                });
            });
            let templateList = pageNo === -1
                ? await new Promise((resolve, reject) => {
                    template_info_model_1.default.find(conditions).select(selectOpt).sort({ updateTime: -1 }).exec((err, res) => {
                        err ? reject(err) : resolve(res);
                    });
                })
                : await new Promise((resolve, reject) => {
                    template_info_model_1.default.aggregate([
                        { $match: conditions },
                        { $sort: { updateTime: -1 } },
                        { $skip: pageNo * pageSize },
                        { $limit: pageSize }
                    ]).exec((err, res) => {
                        err ? reject(err) : resolve(res);
                    });
                });
            return { totalCount, templateList };
        }
        catch (e) {
            throw e;
        }
    }
    static getTplListByTplCodes(tplCodes) {
        let selectOpt = '-acceptBars -pluginBars -adapterBars -dependenciesBar -barDefaultPropsMap -nsTransferMap -children -templateConfig';
        return new Promise((resolve, reject) => {
            template_info_model_1.default.find({ templateCode: { $in: tplCodes } }).select(selectOpt).exec((err, res) => {
                err ? reject(err) : resolve(res && res.length ? JSON.parse(JSON.stringify(res)) : []);
            });
        });
    }
    static getPublishType(id) {
        let conditions = mongoose.Types.ObjectId.isValid(id)
            ? { $or: [{ _id: id }, { templateCode: id }] }
            : { templateCode: id };
        return new Promise((resolve, reject) => {
            template_info_model_1.default.findOne(conditions).select('publishType').lean().exec((err, res) => {
                err ? reject(err) : resolve((res && res.publishType) || '');
            });
        });
    }
}
exports.default = TemplateInfoService;
